package io.renren.common.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil {
    public ExcelUtil() {
    }

    public static Workbook getWorkbook(String fileName) throws IOException {
        Workbook workbook = null;
        if (null != fileName) {
            String fileType = fileName.substring(fileName.lastIndexOf(46), fileName.length());
            FileInputStream fileStream = new FileInputStream(new File(fileName));
            Throwable var4 = null;

            try {
                if (".xls".equalsIgnoreCase(fileType.trim())) {
                    workbook = new HSSFWorkbook(fileStream);
                } else if (".xlsx".equalsIgnoreCase(fileType.trim())) {
                    workbook = new XSSFWorkbook(fileStream);
                }
            } catch (Throwable var13) {
                var4 = var13;
                throw var13;
            } finally {
                if (fileStream != null) {
                    if (var4 != null) {
                        try {
                            fileStream.close();
                        } catch (Throwable var12) {
                            var4.addSuppressed(var12);
                        }
                    } else {
                        fileStream.close();
                    }
                }

            }
        }

        return (Workbook)workbook;
    }

    public static Workbook getWorkbook(File file, String fileName) throws IOException {
        Workbook workbook = null;
        if (null != fileName) {
            String fileType = fileName.substring(fileName.lastIndexOf(46), fileName.length());
            FileInputStream fileStream = new FileInputStream(file);
            Throwable var5 = null;

            try {
                if (".xls".equalsIgnoreCase(fileType.trim())) {
                    workbook = new HSSFWorkbook(fileStream);
                } else if (".xlsx".equalsIgnoreCase(fileType.trim())) {
                    workbook = new XSSFWorkbook(fileStream);
                }
            } catch (Throwable var14) {
                var5 = var14;
                throw var14;
            } finally {
                if (fileStream != null) {
                    if (var5 != null) {
                        try {
                            fileStream.close();
                        } catch (Throwable var13) {
                            var5.addSuppressed(var13);
                        }
                    } else {
                        fileStream.close();
                    }
                }

            }
        }

        return (Workbook)workbook;
    }

    public static void setCellValue(Row row, int index, BigDecimal value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value.doubleValue());
        }

    }

    public static void setCellValue(Row row, int index, BigDecimal value) {
        setCellValue(row, index, (BigDecimal)value, (String)null);
    }

    public static void setCellValue(Row row, int index, Long value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value.doubleValue());
        }

    }

    public static void setCellValue(Row row, int index, Long value) {
        setCellValue(row, index, (Long)value, (String)null);
    }

    public static void setCellValue(Row row, int index, Integer value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value.doubleValue());
        }

    }

    public static void setCellValue(Row row, int index, Integer value) {
        setCellValue(row, index, (Integer)value, (String)null);
    }

    public static void setCellValue(Row row, int index, Short value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value.doubleValue());
        }

    }

    public static void setCellValue(Row row, int index, Short value) {
        setCellValue(row, index, (Short)value, (String)null);
    }

    public static void setCellValue(Row row, int index, Date value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value);
        }

    }

    public static void setCellValue(Row row, int index, Date value) {
        setCellValue(row, index, (Date)value, (String)null);
    }

    public static void setCellValue(Row row, int index, boolean value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        cell.setCellValue(value);
    }

    public static void setCellValue(Row row, int index, boolean value) {
        setCellValue(row, index, value, (String)null);
    }

    public static void setCellValue(Row row, int index, Calendar value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value);
        }

    }

    public static void setCellValue(Row row, int index, Calendar value) {
        setCellValue(row, index, (Calendar)value, (String)null);
    }

    public static void setCellValue(Row row, int index, double value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        cell.setCellValue(value);
    }

    public static void setCellValue(Row row, int index, double value) {
        setCellValue(row, index, value, (String)null);
    }

    public static void setCellValue(Row row, int index, RichTextString value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value);
        }

    }

    public static void setCellValue(Row row, int index, RichTextString value) {
        setCellValue(row, index, (RichTextString)value, (String)null);
    }

    public static void setCellValue(Row row, int index, String value, String format) {
        Cell cell = row.createCell(index);
        if (format != null) {
            Workbook workbook = row.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat(format));
            cell.setCellStyle(cellStyle);
        }

        if (value != null) {
            cell.setCellValue(value);
        }

    }

    public static void setCellValue(Row row, int index, String value) {
        setCellValue(row, index, (String)value, (String)null);
    }

}

